package com.zzf.activitytest2

import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.Button
import android.widget.Toast
import kotlinx.android.synthetic.main.first_layout.*
import java.lang.StringBuilder

class FirstActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.first_layout)

        Log.d("FirstActivityaa",this.toString())

        button1.setOnClickListener {

//            隐式打开一个Activity
//            val intent = Intent("com.example.activityTest2.ACTION_START")
//            intent.addCategory("com.example.activityTest2.MY_CATEGORY")
//            startActivity(intent)

//            打开一个网页
//            val intent = Intent(Intent.ACTION_VIEW)
//            intent.data = Uri.parse("https://www.baidu.com")
//            startActivity(intent)

//            拨打电话
//            val intent = Intent(Intent.ACTION_DIAL)
//            intent.data = Uri.parse("tel:10086")
//            startActivity(intent)

//            启动一个Activity，并传递数据
//            val data = "Hello SecondActivity"
//            val intent = Intent(this,SecondActivity::class.java)
//            intent.putExtra("extra_data",data)
//            startActivity(intent)

//            val intent = Intent(this,SecondActivity::class.java)
//            startActivityForResult(intent,1)

//            val intent = Intent(this,SecondActivity::class.java)
//            startActivityForResult(intent,1)

            SecondActivity.actionStart(this,"data1","data2")

            withStandard()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when(requestCode){
            1 -> if (resultCode == RESULT_OK){
                val returnedData = data?.getStringExtra("data_return")
                Log.d("FirstActivity","returned data is $returnedData")
            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.main,menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId){
            R.id.add_item -> Toast.makeText(this, "You clicked Add", Toast.LENGTH_SHORT).show()
            R.id.remove_item -> Toast.makeText(this, "You clicked Remove", Toast.LENGTH_SHORT).show()

        }
        return true
    }

    override fun onRestart() {
        super.onRestart()
        Log.d("FirstActivity","onRestart")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("FirstActivity","onDestory")
    }

    fun withStandard(){
//        val list = listOf("apple","Banana","Orange","Pear","Grape")
//        val builder = StringBuilder()
//        builder.append("start eating fruits.\n")
//        for (fruit in list){
//            builder.append(fruit).append("\n")
//        }
//        builder.append("Ate all fruits.")
//        val result = builder.toString()
//        println(result)

        val list = listOf("apple","Banana","Orange","Pear","Grape")
        val result = with(StringBuilder()){
            append("Start eating fruits.\n")
            for (fruit in list){
                append(fruit).append("\n")
            }
            append("Ate all fruits")
            toString()
        }
        println(result)
    }

}